using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

using System.Reflection;

namespace NICWebApi.Extensions.DependencyInjection.Extensions;

/// <summary>
/// 依赖注入扩展方法
/// </summary>
public static class DependencyInjectionExtensions
{
  /// <summary>
  /// 添加依赖注入服务
  /// </summary>
  /// <param name="services">服务集合</param>
  /// <param name="assemblies">要扫描的程序集</param>
  /// <returns>服务集合</returns>
  public static IServiceCollection AddDependencyInjection(this IServiceCollection services, params Assembly[] assemblies)
  {
    if (services == null)
    {
      throw new ArgumentNullException(nameof(services));
    }

    // 注册特殊接口
    RegisterSpecialInterfaces(services);

    // 扫描并注册其他服务
    DependencyScanner.AddDependencyInjection(services, assemblies);

    return services;
  }

  /// <summary>
  /// 注册特殊接口
  /// </summary>
  /// <param name="services">服务集合</param>
  private static void RegisterSpecialInterfaces(IServiceCollection services)
  {
    // 可以在这里添加其他特殊接口的注册
    // 例如：
    // services.TryAddSingleton<ILoggerFactory, LoggerFactory>();
    // services.TryAddSingleton<IConfiguration>();
  }
}